package com.zhongxi.guoyuan.view.coinShopping;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.coinShop.CoinStoreAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.CoinStoreData;
import com.zhongxi.guoyuan.entity.coinStore.CoinStoreResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

public class CoinStoreListActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private PullToRefreshListView lvList;
    private List<CoinStoreData> data = new ArrayList<>();
    private String type;
    private CoinStoreAdapter adapter;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_coin_store_list);
        initTitle();
        tvTitle.setText("银币消费商家");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new CoinStoreAdapter(context, data);
        lvList.setAdapter(adapter);
        getStore();
    }

    private void getStore() {
        type = "getStore";
        getServer(Url.REPUTATION_STORE, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            CoinStoreResult storeResutlt = gson.fromJson(response, CoinStoreResult.class);
            MyApplication.getInstance().setSpValue(storeResutlt.getSafeobj());
            if(null!=storeResutlt.getData()&&!"".equals(storeResutlt.getData())){
                data.addAll(storeResutlt.getData());
            }
            adapter.notifyDataSetChanged();
            lvList.onRefreshComplete();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
                MyApplication.getInstance().setSpValue(result.getSafeobj());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(context, CoinShoppingDetailActivity.class);
        intent.putExtra("shopid", data.get(position-1).getId());
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

}
